package com.irdstudio.efp.esb.service.impl.hj;

import com.irdstudio.basic.framework.core.constant.ESBConstant;
import com.irdstudio.basic.framework.core.exception.ESBException;
import com.irdstudio.basic.framework.core.util.TraceUtil;
import com.irdstudio.efp.esb.common.client.resp.EsbRespRetInfBean;
import com.irdstudio.efp.esb.common.client.resp.EsbRespServiceBean;
import com.irdstudio.efp.esb.common.client.resp.EsbRespSysHeadBean;
import com.irdstudio.efp.esb.common.constant.ESBClientConstance;
import com.irdstudio.efp.esb.service.bo.req.hj.ReqLoanReleaseBean;
import com.irdstudio.efp.esb.service.bo.resp.hj.RespLoanReleaseBean;
import com.irdstudio.efp.esb.service.client.ESBClientFactory;
import com.irdstudio.efp.esb.service.client.ESBRequestBuilder;
import com.irdstudio.efp.esb.service.client.EsbBaseBean;
import com.irdstudio.efp.esb.service.client.impl.LhxESBBeanCreator;
import com.irdstudio.efp.esb.service.facade.hj.LoanReleaseService;
import com.irdstudio.efp.nls.common.exception.LoanReleaseException;
import com.irdstudio.efp.nls.service.facade.NlsApplyInfoService;
import com.irdstudio.efp.nls.service.vo.NlsApplyInfoVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.Optional;


/**
 * 需求编号:2019D0519
 * 问题编号:
 * 开发人员: caoxin
 * 创建日期:2019/7/19
 * 功能描述:贷款开立发服务
 * 修改日期:2019/7/19
 * 修改描述:
 */
@Service("loanReleaseService")
public class LoanReleaseServiceImpl implements LoanReleaseService {
    /**
     * 日志
     */
    private Logger logger = LoggerFactory.getLogger(LoanReleaseServiceImpl.class);
    /**
     * 消息模板
     */
    private final String msgTemplate = "message{ }";

    @Override
    public RespLoanReleaseBean loanRelease(ReqLoanReleaseBean reqLoanReleaseBean, String applySeq) throws LoanReleaseException {
        logger.info("======>调用贷款开立发服务接口【"+ applySeq +"_30210001_01】开始<======");
        NlsApplyInfoVO nlsApplyInfoVO = new NlsApplyInfoVO();
        nlsApplyInfoVO.setApplySeq(applySeq);
        nlsApplyInfoVO = nlsApplyInfoService.queryByPk(nlsApplyInfoVO);
        // 结果返回
        RespLoanReleaseBean respLoanReleaseBean = null;
        String retCode = "";
        if (nlsApplyInfoVO != null){
            EsbRespServiceBean esbRespServiceBean = null;
            try {
                EsbBaseBean esbBaseBean = EsbBaseBean.EsbBaseBeanBuilder.
                        anEsbBaseBean().
                        withGLBLSRLNO(nlsApplyInfoVO.getGlobalSerno()).
                        withINSTID(ESBClientConstance.bdInstId).withPLAFMUSRID(nlsApplyInfoVO.getPlatfmUsrID()).withPLATFMID(nlsApplyInfoVO.getPlatfmId()).build();
                esbRespServiceBean = ESBRequestBuilder.buildClient().
                        withEsbBaseBean(esbBaseBean)
                        .withBody(reqLoanReleaseBean)
                        .withTradeNo(ESBConstant.TRADE_CODE_30210001)
                        .withScene(ESBConstant.SCENE_NO_01)
                        .create()
                        .sendAndReceive();
            } catch (ESBException e) {
                e.printStackTrace();
                throw new LoanReleaseException("请求贷款开立发服务接口服务接口调用失败【" + applySeq + "_30220001_11】"+e);
            }
            // 通过系统头判断是否成功系统头
            EsbRespSysHeadBean sysHead =
                    esbRespServiceBean.getSysHead();
            if (Objects.nonNull(sysHead)) {
                // 返回信息数据不为空
                EsbRespRetInfBean esbRespRetInfBean = sysHead.getRetInfArry().stream().findFirst().orElseGet(EsbRespRetInfBean::new);
                retCode = Optional.ofNullable(esbRespRetInfBean.getRetCd()).orElse(StringUtils.EMPTY);
                if (ESBConstant.SUCCESS.equals(retCode)) {
                    String msg2 = "++++====>请求贷款开立发服务接口调用成功<====++++";
                    respLoanReleaseBean = esbRespServiceBean.getBody(RespLoanReleaseBean.class);
                    logger.debug(msg2, msgTemplate);
                } else{
                    String msg3 =  esbRespRetInfBean.getRetMsg();
                    logger.error(msg3);
                    throw new LoanReleaseException(msg3);
                }
            }
        }else {
            if (logger.isErrorEnabled()){
                logger.error("请求贷款开立发服务接口【" + applySeq + "_30220001_11】 获取的全局流水号为空");
            }
            throw new LoanReleaseException("请求贷款开立发服务接口【" + applySeq + "_30220001_11】 获取的全局流水号为空");
        }
        logger.info("======>调用贷款开立发服务接口【"+ applySeq +"_30210001_01】结束<======");
        return respLoanReleaseBean;
    }
    @Autowired
    @Qualifier("nlsApplyInfoService")
    private NlsApplyInfoService nlsApplyInfoService;

}
